<?php
class User_model extends CI_Model {

	public function __construct() {
		$this->load->dbforge();
		$this->createStuTable();
	}

	// 创建学生信息表
	public function createStuTable() {
		// 准备字段信息
		$fields = array(
			'stu_id'    => array(
				'type'           => 'INT',
				'constraint'     => 5,
				'unsigned'       => TRUE,
				'auto_increment' => TRUE,
			),
			'stuname'   => array(
				'type'       => 'VARCHAR',
				'constraint' => '100',
			),
			'stunumber' => array(
				'type'       => 'CHAR',
				'constraint' => '10',
			),
			'stuclass'  => array(
				'type'       => 'int',
				'constraint' => '1',
			),
			'stuurl'    => array(
				'type'       => 'VARCHAR',
				'constraint' => '200',
			),
			'created_at  timestamp default current_timestamp',
			'updated_at TIMESTAMP on update CURRENT_TIMESTAMP NOT NULL',
		);
		// 添加字段
		$this->dbforge->add_field($fields);
		// 添加索引信息
		$this->dbforge->add_key('stu_id', TRUE);
		// 设置其他属性
		$attributes = array('ENGINE' => 'InnoDB');
		// 创建表
		$this->dbforge->create_table('taskinfo', TRUE, $attributes);
	}

	// 插入用户信息
	public function insertStu() {
		// 准备插入的信息
		$data = array(
			// 'stuname'   => $_POST['username'],
			'stuname'   => $this->input->post('username'),
			'stunumber' => $this->input->post('usernumber'),
			'stuclass'  => $this->input->post('userclass'),
			'stuurl'    => $this->input->post('userurl'),
		);

		$this->db->insert('taskinfo', $data);
	}

	// 返回全部学生作业信息
	public function getAll() {
		return $this->db->get('taskinfo')->result_array();
	}
}
